package io.leo.leoui.toasts;

import android.view.Gravity;
import android.widget.Toast;

import io.leo.leoui.R;

/**
 * 一个链式的Toast样式设计器
 *
 * LeoUI将Toast的显示位置，时间等一切后续工作全部交给ToastDesigner
 * 而最基本的布局由LeoUI中的Toast类提供
 */
public class ToastDesigner {
    private ToastTemplate toast;

    public static final int ANIM_LEFT_TO_RIGHT=R.style.to_right;
    public static final int ANIM_RIGHT_TO_LEFT=R.style.to_left;
    public static final int ANIM_BOTTOM_TO_TOP=R.style.to_top;
    public static final int ANIM_TOP_TO_BOTTOM=R.style.to_bottom;
    public static final int ANIM_ALPHA=R.style.alpha;



    public ToastConfig config;
    static class ToastConfig{
        public static class Gravity{
            public int gravity,xoffset,yoffset;

            public Gravity(int gravity, int xoffset, int yoffset) {
                this.gravity = gravity;
                this.xoffset = xoffset;
                this.yoffset = yoffset;
            }
        }

        boolean isRound=false;
        Gravity gravity;
        Integer animate = ANIM_ALPHA;
        int duration=Toast.LENGTH_SHORT;
        boolean immediately=false;
    }

    public ToastDesigner(){
        config = new ToastConfig();
    }


    public ToastDesigner round(){
        config.isRound = true;
        return this;
    }

    public ToastDesigner left(int xoffset,int yoffset){
        config.gravity = new ToastConfig.Gravity(Gravity.LEFT,xoffset,yoffset);
        return this;
    }


    public ToastDesigner right(int xoffset,int yoffset){
        config.gravity = new ToastConfig.Gravity(Gravity.RIGHT,xoffset,yoffset);
        return this;
    }

    public ToastDesigner bottom(int xoffset,int yoffset){
        config.gravity = new ToastConfig.Gravity(Gravity.BOTTOM,xoffset,yoffset);
        return this;
    }

    public ToastDesigner top(int xoffset,int yoffset){
        config.gravity = new ToastConfig.Gravity(Gravity.TOP,xoffset,yoffset);
        return this;
    }

    public ToastDesigner center(int xoffset,int yoffset){
        config.gravity = new ToastConfig.Gravity(Gravity.CENTER,xoffset,yoffset);
        return this;
    }

    public ToastDesigner center(){
        return center(0,0);
    }

    public ToastDesigner animate(int animate){
        config.animate=animate;
        return this;
    }


    public ToastDesigner longTime(){
        config.duration = Toast.LENGTH_LONG;
        return this;
    }

    public ToastDesigner shortTime(){
        config.duration = Toast.LENGTH_SHORT;
        return this;
    }

    /**
     * 立即模式
     * 不等待上一个Toast结束
     * @return
     */
    public ToastDesigner immediately(){
        config.immediately = true;
        return this;
    }


    public void show(ToastTemplate toast){
        if (toast==null)
            throw new RuntimeException("Toast is null...");
        if (config.immediately)
            if (this.toast!=null)
                this.toast.cancel();
        this.toast = toast;
        if (config.isRound)
            toast.getView().setBackgroundResource(R.drawable.radiu_background);
        else toast.getView().setBackgroundResource(R.drawable.rect_background);
        if (config.gravity!=null)
            toast.setGravity(config.gravity.gravity,config.gravity.xoffset,config.gravity.yoffset);
        toast.setAnimate(config.animate);
        toast.setDuration(config.duration);
        toast.show();
    }
}
